package xt3FuturesUser

import (
	"encoding/json"
	"errors"
	"strconv"

	"github.com/astaxie/beego/logs"
)

/**
 * MQ的回调处理
 * 消费MQ的数据
 * 将消费的数据转换成最终的数据
 * 最终的数据写入缓存
**/
var UserInfoConsumerHandle = func(message []byte) (content *RecvMessage, err error) {

	content = new(RecvMessage)
	err = json.Unmarshal(message, content)
	if err != nil {
		logs.Info("[run] Unmarshal model.RecvTopic", string(message))
	}
	return
}

var DepthConsumerHandle = func(symbol, origin string, dataByte []byte, depth *CacheDepth) (data string) {
	// data = *(*string)(unsafe.Pointer(&datab))
	return
}

// 处理ticker
var TickerConsumerHandle = func(message []byte) (data string) {
	data = string(message)
	return
}

// 处理trade
var TradeConsumerHandle = func(message []byte) (data string) {
	data = string(message)
	return
}

/**
 * 自定义处理行情数据的解析
 * 每个行情对应一个独立的解析方式
**/
// Quant-Handle
var DepthHandle = func(message []byte, origin string) (data []byte, err error) {

	depth := &Depth{}

	if err = json.Unmarshal(message, depth); err != nil {
		logs.Debug("[exchange-xt_contract-handle] ", err, string(message))
		return
	}

	//
	depth.MarketAssign(origin)
	if !depth.IsValid() {
		err = errors.New("[xt_contract-DepthHandle] err ")
		return
	}

	data, err = json.Marshal(depth)
	return
}

// Trade-Handle
var TradeHandle = func(message []byte, origin string) (data []byte, err error) {

	trade := &Quotation{}

	if err = json.Unmarshal(message, trade); err != nil {
		logs.Debug("[exchange-xt_contract-handle] ", err, string(message))
		return
	}

	//
	trade.MarketAssign(origin)
	if !trade.IsValid() {
		err = errors.New("[xt_contract-TradeHandle] err ")
		return
	}

	data, err = json.Marshal(trade)
	return
}

// Ticker-Handle
var TickerHandle = func(message []byte, origin string) (data []byte, err error) {

	ticker := &Ticker{}

	if err = json.Unmarshal(message, ticker); err != nil {
		logs.Debug("[xt_contract-TickerHanlde]", err, string(message))
		return
	}

	ticker.MarketAssign(origin)
	if !ticker.IsValid() {
		err = errors.New("[xt_contract-TickerHanlde] err ")
		return
	}

	// 序列化格式
	data, err = json.Marshal(ticker)
	return
}

// depth --> bids
func BidsFormat(data []Bid) []map[string]interface{} {
	if len(data) <= 0 {
		return []map[string]interface{}{
			{"amount": "0", "price": "0", "type": 1},
		}
	}

	bidsMap := make([]map[string]interface{}, len(data))
	for index, value := range data {
		bidsMap[index] = map[string]interface{}{
			"price":  value[0],
			"amount": value[1],
			"type":   1,
		}
	}

	// MarshalError := e.NewMarshalError(json.Marshal(bidsMap))
	// res := MarshalError.Result_Unwrap()
	return bidsMap
}

// depth --> asks
func AsksFormat(data []Ask) []map[string]interface{} {

	if len(data) <= 0 {
		return []map[string]interface{}{
			{"amount": "0", "price": "0", "type": 0},
		}
	}

	asksMap := make([]map[string]interface{}, len(data))
	for index, value := range data {
		asksMap[index] = map[string]interface{}{
			"price":  value[0],
			"amount": value[1],
			"type":   0,
		}
	}

	// MarshalError := e.NewMarshalError(json.Marshal(asksMap))
	// res := MarshalError.Result_Unwrap()
	return asksMap

}

// Strval 获取变量的字符串值
// 浮点型 3.0将会转换成字符串3, "3"
// 非数值或字符类型的变量将会被转换成JSON格式字符串
func Strval(value interface{}) string {
	// interface 转 string
	var key string
	if value == nil {
		return key
	}

	switch value.(type) {
	case float64:
		ft := value.(float64)
		key = strconv.FormatFloat(ft, 'f', -1, 64)
	case float32:
		ft := value.(float32)
		key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
	case int:
		it := value.(int)
		key = strconv.Itoa(it)
	case uint:
		it := value.(uint)
		key = strconv.Itoa(int(it))
	case int8:
		it := value.(int8)
		key = strconv.Itoa(int(it))
	case uint8:
		it := value.(uint8)
		key = strconv.Itoa(int(it))
	case int16:
		it := value.(int16)
		key = strconv.Itoa(int(it))
	case uint16:
		it := value.(uint16)
		key = strconv.Itoa(int(it))
	case int32:
		it := value.(int32)
		key = strconv.Itoa(int(it))
	case uint32:
		it := value.(uint32)
		key = strconv.Itoa(int(it))
	case int64:
		it := value.(int64)
		key = strconv.FormatInt(it, 10)
	case uint64:
		it := value.(uint64)
		key = strconv.FormatUint(it, 10)
	case string:
		key = value.(string)
	case []byte:
		key = string(value.([]byte))
	default:
		newValue, _ := json.Marshal(value)
		key = string(newValue)
	}

	return key
}
